package com.dz.scrm.mvp.ui.mine

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.Typeface
import android.net.http.SslError
import android.os.Bundle
import android.util.Log
import android.view.View
import android.webkit.*
import com.dz.scrm.R
import com.dz.scrm.constant.ACTIVITY_PRIVACY_AGREEMENT
import com.dz.scrm.constant.ACTIVITY_USER_AGREEMENT
import com.dz.scrm.constant.INTENT_URL
import com.dz.scrm.constant.INTENT_URL_TYPE
import com.dz.scrm.mvp.presenter.DZPresent
import com.dz.scrm.mvp.ui.DZActivity
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.activity_html.*
import me.jessyan.art.mvp.IView
import me.jessyan.art.mvp.Message
import me.jessyan.art.utils.ArtUtils

/**
 * 作者：fanda
 * 日期：2021/4/23 15:40
 * 描述：Html 相关的界面
 */
class HtmlActivity : DZActivity<DZPresent>(), IView {

    private var mTargetUrl: String? = null

    override fun initView(savedInstanceState: Bundle?) = R.layout.activity_html

    override fun initData(savedInstanceState: Bundle?) {
        tv_title.typeface = Typeface.defaultFromStyle(Typeface.BOLD);//加粗
        initWebView()
        if (intent.hasExtra(INTENT_URL)) {
            mTargetUrl = intent.getStringExtra(INTENT_URL)
            Log.e("HH","mTargetUrl "+mTargetUrl);
//            if (mTargetUrl.isNullOrEmpty()) {
                if (intent.getStringExtra(INTENT_URL_TYPE) == ACTIVITY_PRIVACY_AGREEMENT) {
//                    mTargetUrl = "https://www.zrloan.cn/adflow_h/regprotocol"
                    tv_title.text = "用户协议"
                } else if (intent.getStringExtra(INTENT_URL_TYPE) == ACTIVITY_USER_AGREEMENT) {
//                    mTargetUrl = "https://www.zrloan.cn/adflow_h/regprotocol"
                    tv_title.text = "隐私政策"
                }
//            }
            webview.loadUrl(mTargetUrl!!)
        }
        initListeners()
    }

    private fun initListeners() {
        iv_back.setOnClickListener { finish() }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebView() {
        webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
        webview.apply {
            settings.setGeolocationEnabled(true)
            webViewClient = object : WebViewClient() {
                override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                    showLoading()
                }

                override fun onPageFinished(view: WebView?, url: String?) {
                    hideLoading()
                }

                override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
                    super.onReceivedError(view, request, error)
                }

                override fun onReceivedHttpError(view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse?) {
                    super.onReceivedHttpError(view, request, errorResponse)
                }

                override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
                    super.onReceivedSslError(view, handler, error)

                }
            }
//            settings.allowFileAccess = true
//            settings.setAllowFileAccessFromFileURLs(true)
            settings.javaScriptEnabled = true
            settings.domStorageEnabled = true //设置适应Html5
        }
    }

    override fun obtainPresenter() = DZPresent(ArtUtils.obtainAppComponentFromContext(this), RxPermissions(this))

    override fun handleMessage(message: Message) {
    }

    override fun showMessage(message: String) {
    }

}